using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Amazon;

public partial class user_addBook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		Books.Text = "";
		if (Request.QueryString["addBookID"] != null)
		{
			string[] myStringArray = getAmazonLookup(Request.QueryString["addBookID"]);//addBook(Request.QueryString["addBookID"]);
			
			Books.Text = "Added " + myStringArray[6] + " by " + myStringArray[0] + 
				" as a " + category.Text + " to your account.";
		}
    }

	protected void findBook(object sender, EventArgs e)
	{
		String inputType = searchBy.Text;
		String inputText = searchInput.Text;
		Books.Text = "";

		List<string[]> myResultList = getAmazonSearch(inputText, inputType);
		foreach (string[] myStringArray in myResultList)
		{
			//Order is: Author, Description, ImageURL, ISBN, numPages, Publisher, Title
			printBook(myStringArray[6], myStringArray[0], "Something Else", myStringArray[3], myStringArray[2]);
		}
		//printBook("My Title", "My Author", "My Other", inputText);
		//printBook("My Title", "My Author", "My Other", "019238344");
		//printBook("My Title", "My Author", "My Other", "019238344");
	}

	protected void printBook(String title, String author, String other, String isbnNumber, String imageURL)
	{
		String tempHolder = Books.Text;
		tempHolder += "<div class=\"bookscontainerbook\">";
		tempHolder += "<div class=\"bookscontainerbookcontent\">";
		tempHolder += "<img id=\"Img"+isbnNumber+"\" class=\"bookscontainerbookimage\" alt=\"BookImage\" src=\""+imageURL+"\" runat=\"server\" />";
		tempHolder += "<div class=\"bookscontainerbooktitle\">"+title+"</div>";
		tempHolder += "<div class=\"bookscontainerbookauthor\">by "+author+"</div>";
		tempHolder += "<div class=\"bookscontainerbookdetails\">"+other+"</div>";
		tempHolder += "</div>";
		tempHolder += "<div class=\"bookscontainerbookcommands\">";
		//LinkButton tempLink = new LinkButton();
		//tempLink.CommandName = "addBook";
		//tempLink.CommandArgument = isbnNumber.ToString();
		//tempLink.Text = "Add This Book";
		//tempHolder += "<asp:LinkButton OnClick=\"addBook\" CommandArgument=\"12345\" text=\"Add Book\" runat=\"server\" />";
		tempHolder += "<a id=\"A7\" href=\"?addBookID="+isbnNumber+"\" runat=\"server\">Add This Book</a>";
		tempHolder += "</div>";
		tempHolder += "</div>\n\n";
		Books.Text = tempHolder;
	}

	private List<string[]> getAmazonSearch(String textInput, String searchKind)
	{
		System.Collections.Generic.List<string[]> myBookList = new List<string[]>();

		try
		{

			AWSECommerceService service = new AWSECommerceService();

			ItemSearch itemSearch = new ItemSearch();


			/******************************************************************/


			ItemSearchRequest request = new ItemSearchRequest();

			itemSearch.SubscriptionId = "0606YS0TEVY33S241T82";

			request.ResponseGroup = new string[] { 
"EditorialReview" ,
"Images" , 
"ItemAttributes" , 
"Small" 
};

			request.SearchIndex = "Books";
			//request.Title = "Harry+Potter";
			switch (searchKind)
			{
				case "Title": request.Title = textInput;
					break;
				case "Author": request.Author = textInput;
					break;
				case "Keyword":
				default:
					request.Keywords = textInput;
					break;
			}


			// Setup request 
			itemSearch.Request = new ItemSearchRequest[] { request };

			// Get the response 
			ItemSearchResponse response = service.ItemSearch(itemSearch);
			if (response == null)
				throw new Exception("Server Error - no response recieved!");

			Items[] itemsArray = response.Items;

			// Check for errors, stop at first error 
			if (itemsArray == null)
				throw new Exception("Server Error - empty response!");
			if (itemsArray[0].Request.Errors != null)
				throw new Exception(itemsArray[0].Request.Errors[0].Message);


			// ================== RESPONSE GROUP ================= 

			foreach (Items items in itemsArray)
			{

				if (items != null)
				{
					//Console.WriteLine("TotalPages: {0}", items.TotalPages);
					//Console.WriteLine("TotalResults: {0}", items.TotalResults);

					/********************************************************** 
					  * Items::Item
					/**********************************************************/
					Item[] itemArray = items.Item;

					if (itemArray != null)
					{
						int itemCount = 0;
						foreach (Item item in itemArray)
						{
							itemCount++;
							if (item != null)
							{

								string myAuthor = "";
								string myDescription = "";


								//Console.WriteLine("====================New Item {0}====================", itemCount);
								//Console.WriteLine("ASIN: {0}", item.ASIN);
								//Console.WriteLine("DetailPageURL: {0}", item.DetailPageURL);

								/********************************************************** 
								  * Item::ItemAttributes
								/**********************************************************/
								ItemAttributes itemAttributes = item.ItemAttributes;

								if (itemAttributes != null)
								{
									//Console.WriteLine(" === ItemAttributes === ");

									string[] authorArray = itemAttributes.Author;

									if (authorArray != null)
									{

										myAuthor = authorArray[0];

										foreach (string author in authorArray)
										{
											//Console.WriteLine("Author: " + author);
										}
									}

									//Console.WriteLine("Edition: {0}", itemAttributes.Edition);
									//Console.WriteLine("ISBN: {0}", itemAttributes.ISBN);
									//Console.WriteLine("Label: {0}", itemAttributes.Label);

									//Console.WriteLine("NumberOfPages: {0}", itemAttributes.NumberOfPages);
									//Console.WriteLine("Publisher: {0}", itemAttributes.Publisher);
									//Console.WriteLine("ReadingLevel: {0}", itemAttributes.ReadingLevel);
									//Console.WriteLine("Title: {0}", itemAttributes.Title);
									//Console.WriteLine("UPC: {0}", itemAttributes.UPC);

								}

								/********************************************************** 
								  * Item::EditorialReviews
								/**********************************************************/
								EditorialReview[] editorialReviewsArray = item.EditorialReviews;

								if (editorialReviewsArray != null)
								{

									foreach (EditorialReview editorialReviews in editorialReviewsArray)
									{

										if ((editorialReviews != null) && (editorialReviews.Source == "Book Description"))
										{
											//Console.WriteLine("Content: {0}", editorialReviews.Content);
											//Console.WriteLine("Source: {0}", editorialReviews.Source);

											myDescription = editorialReviews.Content;

										}
									}
								}


								/********************************************************** 
								  * Item::SmallImage
								/**********************************************************/
								Amazon.Image smallImage = item.SmallImage;

								if (smallImage != null)
								{
									//Console.WriteLine(" === SmallImage === ");

									/********************************************************** 
									  * SmallImage::Height
									/**********************************************************/
									DecimalWithUnits height = smallImage.Height;

									if (height != null)
									{
										//Console.WriteLine(" === Height === ");
										//Console.WriteLine("Units: {0}", height.Units);
										//Console.WriteLine("Value: {0}", height.Value);

									}

									/********************************************************** 
									  * SmallImage::Width
									/**********************************************************/
									DecimalWithUnits width = smallImage.Width;

									if (width != null)
									{
										//Console.WriteLine(" === Width === ");
										//Console.WriteLine("Units: {0}", width.Units);
										//Console.WriteLine("Value: {0}", width.Value);

									}
									//Console.WriteLine("URL: {0}", smallImage.URL);

								}

								

								//Put information into Array
								//Order is: Author, Description, ImageURL, ISBN, numPages, Publisher, Title
								myBookList.Add(new string[] { 
										myAuthor ,
										myDescription ,
										item.SmallImage.URL ,
										item.ItemAttributes.ISBN ,
										item.ItemAttributes.NumberOfPages ,
										item.ItemAttributes.Publisher ,
										item.ItemAttributes.Title });

							}
						}
					}
				}
			} // End of Response Group

		}
		catch (Exception ex)
		{

			//Console.WriteLine(ex.ToString());
		}

		return myBookList;
	}

	public string[] getAmazonLookup(string isbnInput)
	{
		string[] myStringArray = new string[7];
		try
		{

			AWSECommerceService service = new AWSECommerceService();

			ItemLookup itemLookup = new ItemLookup();


			/******************************************************************/


			ItemLookupRequest request = new ItemLookupRequest();

			itemLookup.SubscriptionId = "0606YS0TEVY33S241T82";

			request.ResponseGroup = new string[] { 
"EditorialReview" , 
"Images" , 
"ItemAttributes" , 
"Small" 

};

			//request.SearchIndex = "Books";
			request.ItemId = new string[] { isbnInput };
			//request.IdType = ASIN;


			// Setup request 
			itemLookup.Request = new ItemLookupRequest[] { request };

			// Get the response 
			ItemLookupResponse response = service.ItemLookup(itemLookup);
			if (response == null)
				throw new Exception("Server Error - no response recieved!");

			Items[] itemsArray = response.Items;

			// Check for errors, stop at first error 
			if (itemsArray == null)
				throw new Exception("Server Error - empty response!");
			if (itemsArray[0].Request.Errors != null)
				throw new Exception(itemsArray[0].Request.Errors[0].Message);


			// ================== RESPONSE GROUP ================= 

			foreach (Items items in itemsArray)
			{

				if (items != null)
				{
					//Console.WriteLine("TotalPages: {0}", items.TotalPages);
					//Console.WriteLine("TotalResults: {0}", items.TotalResults);

					/********************************************************** 
					  * Items::Item
					/**********************************************************/
					Item[] itemArray = items.Item;

					if (itemArray != null)
					{
						int itemCount = 0;
						foreach (Item item in itemArray)
						{
							itemCount++;
							if (item != null)
							{

								string myAuthor = "";
								string myDescription = "";


								//Console.WriteLine("====================New Item {0}====================", itemCount);
								//Console.WriteLine("ASIN: {0}", item.ASIN);
								//Console.WriteLine("DetailPageURL: {0}", item.DetailPageURL);

								/********************************************************** 
								  * Item::ItemAttributes
								/**********************************************************/
								ItemAttributes itemAttributes = item.ItemAttributes;

								if (itemAttributes != null)
								{
									//Console.WriteLine(" === ItemAttributes === ");

									string[] authorArray = itemAttributes.Author;

									if (authorArray != null)
									{

										myAuthor = authorArray[0];

										foreach (string author in authorArray)
										{
											//Console.WriteLine("Author: " + author);
										}
									}

									//Console.WriteLine("Edition: {0}", itemAttributes.Edition);
									//Console.WriteLine("ISBN: {0}", itemAttributes.ISBN);
									//Console.WriteLine("Label: {0}", itemAttributes.Label);

									//Console.WriteLine("NumberOfPages: {0}", itemAttributes.NumberOfPages);
									//Console.WriteLine("Publisher: {0}", itemAttributes.Publisher);
									//Console.WriteLine("ReadingLevel: {0}", itemAttributes.ReadingLevel);
									//Console.WriteLine("Title: {0}", itemAttributes.Title);
									//Console.WriteLine("UPC: {0}", itemAttributes.UPC);

								}

								/********************************************************** 
								  * Item::EditorialReviews
								/**********************************************************/
								EditorialReview[] editorialReviewsArray = item.EditorialReviews;

								if (editorialReviewsArray != null)
								{

									foreach (EditorialReview editorialReviews in editorialReviewsArray)
									{

										if ((editorialReviews != null) && (editorialReviews.Source == "Book Description"))
										{
											//Console.WriteLine("Content: {0}", editorialReviews.Content);
											//Console.WriteLine("Source: {0}", editorialReviews.Source);

											myDescription = editorialReviews.Content;

										}
									}
								}


								/********************************************************** 
								  * Item::SmallImage
								/**********************************************************/
								Amazon.Image smallImage = item.SmallImage;

								if (smallImage != null)
								{
									//Console.WriteLine(" === SmallImage === ");

									/********************************************************** 
									  * SmallImage::Height
									/**********************************************************/
									DecimalWithUnits height = smallImage.Height;

									if (height != null)
									{
										//Console.WriteLine(" === Height === ");
										//Console.WriteLine("Units: {0}", height.Units);
										//Console.WriteLine("Value: {0}", height.Value);

									}

									/********************************************************** 
									  * SmallImage::Width
									/**********************************************************/
									DecimalWithUnits width = smallImage.Width;

									if (width != null)
									{
										//Console.WriteLine(" === Width === ");
										//Console.WriteLine("Units: {0}", width.Units);
										//Console.WriteLine("Value: {0}", width.Value);

									}
									//Console.WriteLine("URL: {0}", smallImage.URL);

								}

								//Put information into Array
								//Order is: Author, Description, ImageURL, ISBN, numPages, Publisher, Title
								myStringArray = new string[] { 
										myAuthor ,
										myDescription ,
										item.SmallImage.URL ,
										item.ItemAttributes.ISBN ,
										item.ItemAttributes.NumberOfPages ,
										item.ItemAttributes.Publisher ,
										item.ItemAttributes.Title };

							}
						}
					}
				}
			} // End of Images

		}
		catch (Exception ex)
		{

			Console.WriteLine(ex.ToString());
		}

		return myStringArray;
	}
}
